﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

using HAApplication = System.Windows.Application;
using HA.ModFlow.Application;

using HANetwork;

using HAUtil;

namespace HA.ModFlow.Presentation
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : HAApplication
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);



            //注册信息  当双击文件时自动调用程序打开
            //this.RegisterSetup();


            this.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;

            this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);



            //验证用户操作系统版本

            //vista以及以后的版本
            if (System.Environment.OSVersion.Version.Major >= 6)
            {

            }
            else
            {
                //vista之前的版本


 
            }

            //bool b = isCheck();
            //if (b)
            //{
            //}
            //else
            //{
            //    MessageBox.Show("执行关闭程代码....");
            //}
            
           

            if (e.Args.Length>0)
            {

           
            }


            ///加入特定的验证逻辑代码
            ApplicationService.DefalutService.InitModules();

        }

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            //意外发生

            ProjectService.DefaultService.Save();

           
        }


        //private bool isCheck()
        //{
        //    AustalSystem.NetWork.winLog win = new AustalSystem.NetWork.winLog();
        //    if (win.ShowDialog().Value)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        private void RegisterSetup()
        {
            string extendName = ".hmf";

            bool isFileTypeRegistered = false;
           
            isFileTypeRegistered = FileTypeRegister.FileTypeRegistered(extendName );

            if (isFileTypeRegistered == false)
            {
                //如果未进行过注册

                FileTypeRegInfo fileInfo = new FileTypeRegInfo();

                fileInfo.ExtendName = extendName;
                fileInfo.Description = "HAModflow";
                //fileInfo.IcoPath = "C:\\Documents and Settings\\Administrator\\桌面\\vtk.jpg";
                fileInfo.IcoPath = ApplicationService.DefalutService.GetApplicationPath(ApplicationPath.APPPATH) + "HA.ModFlow.Presentation.exe" + ",0";
               
                fileInfo.ExePath = ApplicationService.DefalutService.GetApplicationPath(ApplicationPath.APPPATH ) + "HA.ModFlow.Presentation.exe";



                FileTypeRegister.RegisterFileType(fileInfo);


            }
            else  
            {

                //更新注册信息

                FileTypeRegInfo fileInfo = new FileTypeRegInfo();

                fileInfo.ExtendName = extendName;
                fileInfo.Description = "HAModflow";
                //fileInfo.IcoPath = "C:\\Documents and Settings\\Administrator\\桌面\\vtk.jpg";
                fileInfo.IcoPath = ApplicationService.DefalutService.GetApplicationPath(ApplicationPath.APPPATH) + "HA.ModFlow.Presentation.exe" + ",0";
               
              
                fileInfo.ExePath = ApplicationService.DefalutService.GetApplicationPath(ApplicationPath.APPPATH) + "HA.ModFlow.Presentation.exe";



                FileTypeRegister.UpdateFileTypeRegInfo (fileInfo);
            
            }





          


        
        }




        protected override void OnExit(ExitEventArgs e)
        {

            ApplicationService.DefalutService.DisposeModules();

            base.OnExit(e);
        }

        protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
        {

            //加入意外关闭程序的代码

            

            ProjectService.DefaultService.Save();
            
            base.OnSessionEnding(e);
        }

    }
}
